<?php


namespace app\admin\controller;


use app\admin\model\FlowScheme;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
use think\Request;

class Form extends BaseController
{
    protected $group = [];

    public function initialize()
    {
        parent::initialize();
        $this->group = $this->auth->getGroups();//获取当前角色
    }


    public function super_manage(Request $request)
    {
        if ($request->isAjax()) {
            //获取每页显示的条数
            $limit = $request->param('limit');
            //获取当前页数
            $page = $request->param('page');
            $where = [];
            if ($request->has('title') && !empty($request->get('title'))) {
                $where[] = array("title", "like", "%" . $request->get('title') . "%");
            }
            $result = Db::connect('old_oa')->name('cjbd')->where($where)->order('crea_time desc')->paginate($limit, false, ['page' => $page])->toArray();
            foreach ($result['data'] as $key => $value) {
                $result['data'][$key]['addname'] = $value['addname'] ?? 'unknown';
                $result['data'][$key]['status_type'] = $value['status'] ? '审核通过' : '待审核';
            }
            return json(['code' => 200, 'msg' => '', 'total' => $result['total'], 'data' => $result['data']]);
        }
        return View::fetch();
    }

    public function edit(Request $request)
    {
        if ($request->isAjax()) {
            $data = [
                $request->param('field') => $request->param('value'),
                'id' => $request->param('id'),
            ];
            Db::connect('old_oa')->name('cjbd')->update($data);
            return json(['code' => 200, 'msg' => '修改成功', 'data' => '']);
        }
    }

    //详情
    public function read(Request $request)
    {
        $id = $request->param('id');
        if ($request->isAjax()) {
            $id = $request->param('id');
            //获取每页显示的条数
            $limit = $request->param('limit');
            //获取当前页数
            $page = $request->param('page');
            $result = Db::connect('old_oa')->name('cjbds')->where('id_cjbd', '=', $id)->paginate($limit, false, ['page' => $page])->toArray();
            return json(['code' => 200, 'msg' => '', 'total' => $result['total'], 'data' => $result['data']]);
        }
        View::assign('id', $id);
        return View::fetch();
    }

    public function del($ids = ""){
        if ($ids){
          Db::connect('old_oa')->name('cjbd')->delete([$ids]);
        }
        return json(['code' => 200, 'msg' => '删除成功', 'data' => '']);
    }


    //新增
    public function add(Request $request){
        if ($request->isAjax()){
            $data = [
                'title'=>$request->param('title'),
                'addid'=>$this->auth->id,
                'addname'=>$this->auth->nickname,
                'crea_time'=>date('Y-m-d H:s:i',time())
            ];
            $result = Db::connect('old_oa')->name('cjbd')->save($data);
            if ($result){
                return json(['code' => 200, 'msg' => '创建成功', 'data' => '']);
            }
        }
        return View::fetch();
    }


    //流转表单
    public function wander(Request $request){
        View::assign('user_id',34); //用户id
        return View::fetch("form/wander/index");
    }
}